<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--钱包金额数据-->
      <el-col :span="24" :xs="24">
        <!-- 搜索 -->
        <template>
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
            <el-form-item label="状态" prop="status">
              <el-select
                v-model="queryParams.status"
                placeholder="钱包金额状态"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="店铺ID" prop="shopId" v-if="$store.getters && $store.getters.admin">
              <el-input
                v-model="queryParams.shopId"
                placeholder="请输入店铺ID"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="dateRange"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </template>

        <!-- 按钮操作栏 -->
        <template>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['market:wallet:add']"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['market:wallet:edit']"
              >修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['market:wallet:remove']"
              >删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>
        </template>

        <!-- 表格 -->
        <template>
          <el-table v-loading="loading" :data="walletList" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column type="index" label="序号" width="50" align="center" />
            <!-- <el-table-column label="编号" align="center" key="walletId" prop="walletId" /> -->
            <el-table-column label="价格" align="center" key="walletPrice" prop="walletPrice" sortable="custom" :sort-orders="['descending', 'ascending']"/>
            <el-table-column label="价值" align="center" key="walletValue" prop="walletValue" sortable="custom" :sort-orders="['descending', 'ascending']"/>
            <el-table-column label="排序" align="center" key="walletSort" prop="walletSort" sortable="custom" :sort-orders="['descending', 'ascending']"/>
            <el-table-column label="状态" align="center" key="status">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.status"
                  active-value="0"
                  inactive-value="1"
                  @change="handleStatusChange(scope.row)"
                ></el-switch>
              </template>
            </el-table-column>
            <el-table-column label="店铺名称" prop="shopInfo.shopName" min-width="120" v-if="$store.getters && $store.getters.admin" />
            <el-table-column
              label="操作"
              align="center"
              width="160"
              fixed="right"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['market:wallet:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['market:wallet:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </template>

      </el-col>
    </el-row>

    <!-- 弹窗 -->
    <template>
      <!-- 添加或修改钱包金额配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="900px" :close-on-click-modal="false" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="钱包金额价格" prop="walletPrice">
                <el-input type="number" v-model="form.walletPrice" placeholder="钱包金额价格" maxlength="30" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="钱包金额价值" prop="walletValue">
                <el-input type="number" v-model="form.walletValue" placeholder="钱包金额价值" maxlength="30" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="排序" prop="walletSort">
                <el-input v-model="form.walletSort" type="number" placeholder="请输入排序" maxlength="30" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态">
                <el-radio-group v-model="form.status">
                  <el-radio
                    v-for="dict in dict.type.sys_normal_disable"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </template>
  </div>
</template>

<script>
import { listWallet, getWallet, delWallet, addWallet, updateWallet, changeWalletStatus } from "@/api/market/wallet";

export default {
  dicts: ['sys_normal_disable'],
  components: { },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 钱包金额表格数据
      walletList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        walletValue: undefined,
        status: undefined,
        shopId: undefined,
        orderByColumn: 'walletSort',
        isAsc: 'asc',
      },
      // 表单校验
      rules: {
        walletPrice: [
          { required: true, message: '金格必须填写', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (!value) {
                return callback();
              }
              if (!/^(\d+|\d+.\d{1,2})$/.test(value)) {
                callback(new Error('请输入正确的金额格式'));
              } else {
                callback();
              }
            },
            trigger: 'blur'
          }
        ],
        walletValue: [
          { required: true, message: '价值必须填写', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (!value) {
                return callback();
              }
              if (!/^(\d+|\d+.\d{1,2})$/.test(value)) {
                callback(new Error('请输入正确的金额格式'));
              } else {
                callback();
              }
            },
            trigger: 'blur'
          }
        ],
      },
    };
  },
  
  computed: {
  },
  watch: {
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询钱包金额列表 */
    getList() {
      this.loading = true;
      listWallet(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.walletList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 钱包金额状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.walletValue + '"钱包金额吗？').then(function() {
        return changeWalletStatus(row.walletId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        walletId: undefined,
        walletValue: undefined,
        walletPrice: undefined,
        walletSort: undefined,
        appId: undefined,
        status: "0",
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.walletId = undefined;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.walletId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getWallet().then(response => {
        this.open = true;
        this.title = "添加钱包金额";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const walletId = row.walletId || this.ids;
      getWallet(walletId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改钱包金额";
      });
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.form))
          if (this.form.walletId != undefined) {
            updateWallet(params).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWallet(params).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const walletIds = row.walletId || this.ids;
      this.$modal.confirm('是否确认删除钱包金额编号为"' + walletIds + '"的数据项？').then(function() {
        return delWallet(walletIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>